<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>优惠券页</title>
    <style>
        div {
            position: absolute;
            top: 30%;
            left: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
        }
        table,tr,th,td{
            border: 1px solid #5fbdcb;
        }
        table{
            border-collapse: collapse;
        }
    </style>
</head>
<body>
<div>
    <table></table><br>
    <a href="UserPage.jsp">返回</a>
</div>
<script src="https://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script>
<script>
    $(function () {
        Date.prototype.format = function(fmt) {
            var o = {
                "M+" : this.getMonth()+1,                 //月份
                "d+" : this.getDate(),                    //日
                "h+" : this.getHours(),                   //小时
                "m+" : this.getMinutes(),                 //分
                "s+" : this.getSeconds(),                 //秒
                "q+" : Math.floor((this.getMonth()+3)/3), //季度
                "S"  : this.getMilliseconds()             //毫秒
            };
            if(/(y+)/.test(fmt)) {
                fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
            }
            for(var k in o) {
                if(new RegExp("("+ k +")").test(fmt)){
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length===1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
                }
            }
            return fmt;
        }
        let arr1 = [];
        let arr2 = [];
        $.ajax({
            url: "http://localhost:8080/coupon.do",
            type: "post",
            data: {
                method: 'userCoupon1'
            },
            dataType: "text",
            success: function (data) {
                console.log('data:'+data)
                let parse = JSON.parse(data);
                if (parse.length === 0) {
                    alert("没有领取的优惠券");
                    window.location.href = 'UserPage.jsp';
                } else {
                    for (let i = 0; i < parse.length; i++) {
                        arr1.push(parse[i].status);
                        arr2.push(parse[i].time);
                    }
                }
            }
        })
        $.ajax({
            url: "http://localhost:8080/coupon.do",
            type: "post",
            data: {
                method: 'userCoupon2'
            },
            dataType: "text",
            success: function (data) {
                let parse = JSON.parse(data);
                let table = document.querySelector('table');
                for (let i = 0; i < parse.length; i++) {
                    let tr = document.createElement('tr');
                    table.appendChild(tr);
                    let th1 = document.createElement('th');
                    th1.innerText = '店铺名字';
                    tr.appendChild(th1);
                    let td1 = document.createElement('td');
                    td1.innerText = parse[i].storeName;
                    tr.appendChild(td1);
                    let td2 = document.createElement('td');
                    td2.innerText = '优惠券满' + parse[i].min + '减' + parse[i].value;
                    tr.appendChild(td2);
                    let th3 = document.createElement('th');
                    let td3 = document.createElement('td');
                    console.log(arr1[i])
                    if (arr1[i] === 0) {
                        th3.innerText = '过期时间：'
                        tr.appendChild(th3);
                        // let replace = arr2[i].replace(new RegExp("-", "gm"), "/");
                        let time = (new Date(arr2[i])).getTime();
                        let validity = parse[i].validity * 24 * 60 * 60 * 1000;
                        let number = time + validity;
                        td3.innerText = new Date(number).format("yyyy-MM-dd hh:mm:ss");
                    }
                    if (arr1[i]===1){
                        td3.innerText = '已过期';
                    }
                    if (arr1[i]===2){
                        td3.innerText = '已使用';
                    }
                    tr.appendChild(td3);
                }
            }
        })
    })
</script>
</body>
</html>
